{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5130f04",
   "metadata": {},
   "source": [
    "# Project Initialization\n",
    "\n",
    "In this first notebook, we initialize the project by creating the directory tree where inputs and outputs will be stored. Path variables are environment variables that are defined within the `parameters.cfg` file, which is located in your working directory. We also define a number of other environment variables that will be used at later stages of the BPMF workflow."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c69780b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "import BPMF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "798af93d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are using BPMF v2.0.0.alpha1\n"
     ]
    }
   ],
   "source": [
    "print(f\"You are using BPMF v{BPMF.__version__}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f317b11f",
   "metadata": {},
   "outputs": [],
   "source": [
    "config = {}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81efc24d",
   "metadata": {},
   "source": [
    "## Define the path variables\n",
    "\n",
    "NB: path variables can either be relative or absolute paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cebfd0e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# INPUT_PATH: path to root directory where all input data will be stored\n",
    "config[\"INPUT_PATH\"] = \"../BPMF_data\"\n",
    "# OUTPUT_PATH: path to root directory where all outputs produced by BPMF will be stored\n",
    "config[\"OUTPUT_PATH\"] = \"../BPMF_outputs\"\n",
    "# NETWORK_PATH: path to directory where the network metadata file will be stored\n",
    "config[\"NETWORK_PATH\"] = \"../network\"\n",
    "# MOVEOUTS_PATH: path to directory where travel times will be stored\n",
    "config[\"MOVEOUTS_PATH\"] = \"../moveouts\"\n",
    "# NLLOC_INPUT_PATH: path to directory where NLLoc input files will be stored\n",
    "config[\"NLLOC_INPUT_PATH\"] = os.path.join(config[\"INPUT_PATH\"], \"NLLoc_inputs\")\n",
    "# NLLOC_OUTPUT_PATH: path to directory where NLLoc output files will be stored\n",
    "config[\"NLLOC_OUTPUT_PATH\"] = os.path.join(config[\"OUTPUT_PATH\"], \"NLLoc_outputs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d28db41",
   "metadata": {},
   "source": [
    "## Create folder tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c63068a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "for var in [\"INPUT_PATH\", \"OUTPUT_PATH\", \"NETWORK_PATH\", \"MOVEOUTS_PATH\", \"NLLOC_INPUT_PATH\", \"NLLOC_OUTPUT_PATH\"]:\n",
    "    if not os.path.isdir(config[var]):\n",
    "        os.makedirs(config[var])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "697ee07a",
   "metadata": {},
   "source": [
    "## Define the preprocessing parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9f934341",
   "metadata": {},
   "outputs": [],
   "source": [
    "# SAMPLING_RATE_HZ: target sampling rate of the resampled data\n",
    "config[\"SAMPLING_RATE_HZ\"] = 25.\n",
    "# MIN_FREQ_HZ: minimum frequency, in Hertz, of the bandpass filter\n",
    "config[\"MIN_FREQ_HZ\"] = 2.\n",
    "# MAX_FREQ_HZ: maximum frequency, in Hertz, of the bandpass filter\n",
    "config[\"MAX_FREQ_HZ\"] = 12.\n",
    "# DATA_BUFFER_SEC: duration, in seconds, of the buffer data at the start and end of each day\n",
    "config[\"DATA_BUFFER_SEC\"] = 500."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6b17b7e",
   "metadata": {},
   "source": [
    "## Define the backprojection parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1885b512",
   "metadata": {},
   "outputs": [],
   "source": [
    "# N_DEV_BP_THRESHOLD: number of deviations (e.g. rms or mad) above central tendency (e.g. mean or median) for\n",
    "#                     backprojection detection threshold (not used in this tutorial)\n",
    "config[\"N_DEV_BP_THRESHOLD\"] = 15."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fea4c84",
   "metadata": {},
   "source": [
    "## Define the template matching parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "437628ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# TEMPLATE_LEN_SEC: template length, in seconds\n",
    "config[\"TEMPLATE_LEN_SEC\"] = 8.\n",
    "# MATCHED_FILTER_STEP_SAMP: step size, in samples, between two correlation coefficient measurements\n",
    "config[\"MATCHED_FILTER_STEP_SAMP\"] = 1\n",
    "# N_DEV_MF_THRESHOLD: number of deviations (e.g. rms or mad) above central tendency (e.g. mean or median) for\n",
    "#                     matched filter detection threshold\n",
    "config[\"N_DEV_MF_THRESHOLD\"] = 8."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce7f9a78",
   "metadata": {},
   "source": [
    "## Miscelleanous"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "768be8d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# NLLOC_BASENAME: basename of NLLoc files\n",
    "config[\"NLLOC_BASENAME\"] = \"NAF\"\n",
    "# BUFFER_EXTRACTED_EVENTS_SEC: duration taken before origin time when reading an event's data\n",
    "config[\"BUFFER_EXTRACTED_EVENTS_SEC\"] = 20."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21631c62",
   "metadata": {},
   "source": [
    "## Write the `parameters.csv` file in current working directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9135bb37",
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert config dictionary to pandas.DataFrame for pretty print\n",
    "config_pd = pd.DataFrame(data=[], index=[], columns=[\"parameter_value\"])\n",
    "config_pd.index.name = \"parameter_name\"\n",
    "for key in config:\n",
    "    config_pd.loc[key] = config[key]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4f56173e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>parameter_value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>parameter_name</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>INPUT_PATH</th>\n",
       "      <td>../BPMF_data</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>OUTPUT_PATH</th>\n",
       "      <td>../BPMF_outputs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NETWORK_PATH</th>\n",
       "      <td>../network</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MOVEOUTS_PATH</th>\n",
       "      <td>../moveouts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NLLOC_INPUT_PATH</th>\n",
       "      <td>../BPMF_data/NLLoc_inputs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NLLOC_OUTPUT_PATH</th>\n",
       "      <td>../BPMF_outputs/NLLoc_outputs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SAMPLING_RATE_HZ</th>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MIN_FREQ_HZ</th>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MAX_FREQ_HZ</th>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DATA_BUFFER_SEC</th>\n",
       "      <td>500.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>N_DEV_BP_THRESHOLD</th>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TEMPLATE_LEN_SEC</th>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MATCHED_FILTER_STEP_SAMP</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>N_DEV_MF_THRESHOLD</th>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NLLOC_BASENAME</th>\n",
       "      <td>NAF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BUFFER_EXTRACTED_EVENTS_SEC</th>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           parameter_value\n",
       "parameter_name                                            \n",
       "INPUT_PATH                                    ../BPMF_data\n",
       "OUTPUT_PATH                                ../BPMF_outputs\n",
       "NETWORK_PATH                                    ../network\n",
       "MOVEOUTS_PATH                                  ../moveouts\n",
       "NLLOC_INPUT_PATH                 ../BPMF_data/NLLoc_inputs\n",
       "NLLOC_OUTPUT_PATH            ../BPMF_outputs/NLLoc_outputs\n",
       "SAMPLING_RATE_HZ                                      25.0\n",
       "MIN_FREQ_HZ                                            2.0\n",
       "MAX_FREQ_HZ                                           12.0\n",
       "DATA_BUFFER_SEC                                      500.0\n",
       "N_DEV_BP_THRESHOLD                                    15.0\n",
       "TEMPLATE_LEN_SEC                                       8.0\n",
       "MATCHED_FILTER_STEP_SAMP                                 1\n",
       "N_DEV_MF_THRESHOLD                                     8.0\n",
       "NLLOC_BASENAME                                         NAF\n",
       "BUFFER_EXTRACTED_EVENTS_SEC                           20.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "config_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e4ceae43",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"BPMF_parameters.cfg\", \"w\") as fparams:\n",
    "    for key in config:\n",
    "        fparams.write(f\"{key} = {config[key]}\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('hy7_py310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.1.-1"
  },
  "vscode": {
   "interpreter": {
    "hash": "221f0e5b1b98151b07a79bf3b6d0c1d306576197d2c4531763770570a29e708e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
